package com.ruihu.admin.third;

import com.ruihu.admin.common.constant.SystemEnvEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * [ApplicationContextHelper]
 *
 * @author yandanyang
 * @version 1.0
 * @since JDK1.8
 */
@Component
public class ApplicationContext implements ApplicationContextAware {

    /**
     * 系统运行环境
     */
    private static SystemEnvEnum SYSTEM_ENV = SystemEnvEnum.DEV_ENV;

    /**
     * 上下文对象实例
     */
    private static org.springframework.context.ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws BeansException {
        if(ApplicationContext.applicationContext == null){
            ApplicationContext.applicationContext  = applicationContext;
        }

        // 获取系统当前的运行环境
        Environment env = applicationContext.getEnvironment();
        String[] activeProfiles = env.getActiveProfiles();
        for (String profile : activeProfiles) {
            if ("dev".equals(profile))
                SYSTEM_ENV = SystemEnvEnum.DEV_ENV;
            else if ("sit".equals(profile))
                SYSTEM_ENV = SystemEnvEnum.SIT_ENV;
            else if ("prod".equals(profile))
                SYSTEM_ENV = SystemEnvEnum.PROD_ENV;
        }
    }

    /**
     * 获取applicationContext
     * @return
     */
    public static org.springframework.context.ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     * @param name
     * @return
     */
    public static Object getBean(String name){
        org.springframework.context.ApplicationContext applicationContext = getApplicationContext();
        if(applicationContext == null){
            return null;
        }
        return applicationContext.getBean(name);
    }

    /**
     * 通过class获取Bean.
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz){
        org.springframework.context.ApplicationContext applicationContext = getApplicationContext();
        if(applicationContext == null){
            return null;
        }
        return applicationContext.getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name,Class<T> clazz){
        org.springframework.context.ApplicationContext applicationContext = getApplicationContext();
        if(applicationContext == null){
            return null;
        }
        return applicationContext.getBean(name, clazz);
    }

    public static boolean isDevEnvironment() {
        return SYSTEM_ENV.equals(SystemEnvEnum.DEV_ENV);
    }

    public static boolean isSitEnvironment() {
        return SYSTEM_ENV.equals(SystemEnvEnum.SIT_ENV);
    }

    public static boolean isProductEnvironment() {
        return SYSTEM_ENV.equals(SystemEnvEnum.PROD_ENV);
    }
}